﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;

namespace EasyLibrary.AR.Classes
{
   public class RotaCoordenada:ActiveRecord
    {
       public int IDRotaCoordenada { get; set; }
       public double Latitude { get; set; }
       public double Longitude { get; set; }
       public int IDRota { get; set; }
       public int Ordem { get; set; }
       public DateTime DataInsercao { get; set; }
       
       public RotaCoordenada()
       {
           
       }

       public RotaCoordenada(int Idrc,double lat,double lng,int idr,int ord,DateTime data)
       {
           this.IDRotaCoordenada = Idrc;
           this.Latitude = lat;
           this.Longitude = lng;
           this.IDRota = idr;
           this.Ordem = ord;
           this.DataInsercao = data;
       }

       public RotaCoordenada(DataRow row)
       {
           this.IDRotaCoordenada = Convert.ToInt32(row["IDRotasCoordenada"]);
           this.Latitude = (double) row["Latitude"];
           this.Longitude = (double) row["Longitude"];
           this.IDRota = (int) row["IDRota"];
           this.Ordem = (int) row["Ordem"];
           this.DataInsercao=(DateTime)row["DataInsercao"];
       }

       

       public IList<RotaCoordenada> GetRotaById(int rotaId)
       {
          var myDataSet = executeCommandOnDataSet("SELECT DISTINCT RotasCoordenadas.IDRotasCoordenada, RotasCoordenadas.Latitude, RotasCoordenadas.Longitude, RotasCoordenadas.IDRota, RotasCoordenadas.DataInsercao, RotasCoordenadas.Ordem FROM Rotas INNER JOIN RotasCoordenadas ON Rotas.IDRota = RotasCoordenadas.IDRota INNER JOIN RotasMarca ON Rotas.IDRota = RotasMarca.RefIDRota INNER JOIN RotasViatura ON Rotas.IDRota = RotasViatura.IDRota WHERE (RotasCoordenadas.IDRota ="+rotaId+")");

          if (myDataSet.Tables[0].Rows.Count == 0)
               return null;

           
           var ret = new List<RotaCoordenada>(myDataSet.Tables[0].Rows.Count);
           foreach (DataRow row in myDataSet.Tables[0].Rows)
           {
               var point = new RotaCoordenada
                               {
                                  // IDRotaCoordenada = (int)row["IDRotasCoordenada"],
                                   Latitude = (double)row["Latitude"],
                                   Longitude=(double)row["Longitude"],
                                 //  IDRota = (int)row["IDRota"],
                                 //  Ordem=(int)row["Ordem"],
                                 //  DataInsercao = (DateTime)row["DataInsercao"]
                               };
               ret.Add(point);
           }
           return ret;
       }

       public override void Save()
       {
           throw new NotImplementedException();
       }
    }
}
